﻿using System;
using System.Collections.Generic;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace ChatBySignalR
{
  

    public class UserInfo
    {
        public string Name { get; set; }
        public string ConnectionId { get; set; }
    }

    public class ChatInfo
    {
        public UserInfo User { get; set; }
        public string Content { get; set; }
        public DateTime PublishTime { get; set; }
    }

    public class ChatSession
    {
        public string SessionName{ get; set; }
        public IList<UserInfo> UserList { get; set; }
        public IList<ChatInfo> Chats { get; set; }
    }


    [HubName("yichat")]
    public class Chat : Hub
    {

        public static List<ChatSession> ChatSessions = new List<ChatSession>();


        public static ChatSession bigSession = new ChatSession()
                                            {
                                                SessionName = "bigChat",
                                                UserList =  new List<UserInfo>(),
                                                Chats = new List<ChatInfo>()
                                            };


        public Chat()
        {
            ChatSessions.Add(bigSession);
        }

        public void Init()
        {
            bigSession.UserList.Add(new UserInfo()
                                        {
                                            ConnectionId = Context.ConnectionId,
                                            Name = Clients.Caller.name
                                        });

            Groups.Add(Context.ConnectionId, bigSession.SessionName);

            Clients.Client(Context.ConnectionId).init(bigSession);

            Clients.Group(bigSession.SessionName).refreshUsers(bigSession);
        }

        public void AddUserToSession()
        {
        }

        public void AddUser()
        {
            //var u = new ClientInfo()
            //            {

            //                UserName =   Clients.Caller.name,
            //                Groups =  new List<string>()
            //                              {
            //                                  Clients.Caller.name,
            //                                  bigGroup
            //                              },
            //                ConnId = Context.ConnectionId
            //            };

            //Users.Add(u);

            //foreach (var user in Users)
            //{
            //    Clients.All.addUser(user.UserName);
            //}
        }


        public void Send(string sessionName, string message)
        {
            Clients.Group(sessionName).displayMessage(sessionName,Clients.Caller.name, message);
        }

       

        public void ChatWith(string userName, string connectionId)
        {

            var newSession= new ChatSession()
                                            {
                                                SessionName = Guid.NewGuid().ToString(),
                                                UserList =  new List<UserInfo>()
                                                    {
                                                       new UserInfo(){ConnectionId = connectionId,Name = userName},
                                                       new UserInfo(){ConnectionId = Context.ConnectionId, Name =Clients.Caller.name }
 
                                                    },
                                                Chats = new List<ChatInfo>()
                                            };

            ChatSessions.Add(newSession);

            Groups.Add(Context.ConnectionId, newSession.SessionName);
            Groups.Add(connectionId, newSession.SessionName);

            Clients.Client(Context.ConnectionId).joinSession(newSession);
            Clients.Client(connectionId).joinSession(newSession);

            Clients.Group(newSession.SessionName).refreshUsers(newSession);
        }
    }

    
}